package cn.itcast.tlias.com.tliaswork.controller;

import cn.itcast.tlias.com.tliaswork.pojo.Emp;
import cn.itcast.tlias.com.tliaswork.pojo.Result;
import cn.itcast.tlias.com.tliaswork.service.EmpService;
import cn.itcast.tlias.com.tliaswork.utils.JWTUtils;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService mService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        log.info("员工登录{}", emp);
        Emp e = mService.login(emp);
        if (e != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", e.getId());
            map.put("name", e.getName());
            map.put("username", e.getUsername());
            String token = JWTUtils.getToken(map);
            return Result.success(token);
        } else {
            return Result.error("用户名或密码输入错误");
        }
    }
}
